package com.yuyou.activity.ts.service.service.impl;

import com.yuyou.activity.ts.service.entity.ActivityTsQuestionConfig;
import com.yuyou.activity.ts.service.entity.ActivityTsUser;
import com.yuyou.activity.ts.service.entity.ActivityTsUserDetail;
import com.yuyou.activity.ts.service.mapper.ActivityTsQuestionConfigMapper;
import com.yuyou.activity.ts.service.mapper.ActivityTsUserDetailMapper;
import com.yuyou.activity.ts.service.mapper.ActivityTsUserMapper;
import com.yuyou.activity.ts.service.mess.ActivityTsUserMess;
import com.yuyou.activity.ts.service.service.IActivityTsUserService;
import com.yuyou.activity.ts.service.vo.req.ReqLoginActivityTs;
import com.yuyou.activity.ts.service.vo.resp.RespExamDetail;
import com.yuyou.activity.ts.service.vo.resp.RespLoginActivityTs;
import com.yuyou.activity.ts.service.vo.resp.RespQuestion;
import com.yuyou.activity.util.bean.ResBoolSimpleInfo;
import com.yuyou.activity.util.enums.ActivityTsUserDetailEnums;
import com.yuyou.activity.util.enums.ActivityTsUserEnums;
import com.yuyou.activity.util.util.DateUtil;
import com.yuyou.activity.util.util.DefindBeanUtils;
import com.yuyou.activity.util.util.Sequence;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by yujin.liu on 2018/12/19.
 */
@Service
public class ActivityTsUserServiceImpl implements IActivityTsUserService{

    @Autowired
    private ActivityTsUserMapper activityTsUserMapper;

    @Autowired
    private ActivityTsQuestionConfigMapper activityTsQuestionConfigMapper;

    @Autowired
    private ActivityTsUserDetailMapper activityTsUserDetailMapper;


    @Override
    public int deleteByPrimaryKey(Long activityTsUserId) {
        return activityTsUserMapper.deleteByPrimaryKey(activityTsUserId);
    }

    @Override
    public int insert(ActivityTsUser record) {
        record.setCreateTime(DateUtil.now());
        return activityTsUserMapper.insert(record);
    }

    @Override
    public ActivityTsUser selectByPrimaryKey(Long activityTsUserId) {
        return activityTsUserMapper.selectByPrimaryKey(activityTsUserId);
    }

    @Override
    public int updateByPrimaryKeySelective(ActivityTsUser record) {
        return activityTsUserMapper.updateByPrimaryKeySelective(record);
    }

    @Override
    @Transactional(rollbackFor=Exception.class,timeout=60)
    public ResBoolSimpleInfo<RespLoginActivityTs> LoginActivityTs(ReqLoginActivityTs reqLoginActivityTs, Integer time) {
        ResBoolSimpleInfo<RespLoginActivityTs> res = new ResBoolSimpleInfo<>();
        RespLoginActivityTs respLoginActivityTs = new RespLoginActivityTs();
        // 查询用户是否已存在
        ActivityTsUser activityTsUser = activityTsUserMapper.selectByProjectCodeAndPhone(reqLoginActivityTs.getProjectCode().trim(), reqLoginActivityTs.getPhone().trim());
        if(null == activityTsUser){
            ActivityTsUser activityTsUserInsert = new ActivityTsUser();
            DefindBeanUtils.copyProperties(reqLoginActivityTs, activityTsUserInsert);
            activityTsUserInsert.setActivityTsUserId(Sequence.getSequence().nextId());
            activityTsUserInsert.setStatus(ActivityTsUserEnums.stauts.preExam.getValue());
            activityTsUserInsert.setTime(time);
            insert(activityTsUserInsert);

            DefindBeanUtils.copyProperties(activityTsUserInsert, respLoginActivityTs);

            // 插入用户题目
            List<ActivityTsQuestionConfig> questionConfigList = activityTsQuestionConfigMapper.findByProjectCode(reqLoginActivityTs.getProjectCode());
            List<ActivityTsUserDetail> activityTsUserDetailList = new ArrayList<>();
            questionConfigList.forEach(q ->{
                ActivityTsUserDetail activityTsUserDetail = new ActivityTsUserDetail();
                activityTsUserDetail.setActivityTsUserDetailId(Sequence.getSequence().nextId());
                activityTsUserDetail.setActivityTsQuestionConfigId(q.getActivityTsQuestionConfigId());
                activityTsUserDetail.setActivityTsUserId(activityTsUserInsert.getActivityTsUserId());
                activityTsUserDetail.setProjectCode(reqLoginActivityTs.getProjectCode());
                activityTsUserDetail.setSort(q.getSort());
                activityTsUserDetail.setStatus(ActivityTsUserDetailEnums.stauts.notAnswer.getValue());
                activityTsUserDetail.setCreateTime(DateUtil.now());

                activityTsUserDetailList.add(activityTsUserDetail);
            });
            activityTsUserDetailMapper.batchInsertActivityTsUserDetail(activityTsUserDetailList);

        }else{
            DefindBeanUtils.copyProperties(activityTsUser, respLoginActivityTs);
        }

        res.setSuccess(true);
        res.setData(respLoginActivityTs);
        return res;
    }

    @Override
    public ResBoolSimpleInfo<RespExamDetail> examDetailOfUser(Long activityTsUserId) {
        ResBoolSimpleInfo<RespExamDetail> res = new ResBoolSimpleInfo<>();
        RespExamDetail respExamDetail = new RespExamDetail();
        // 先查用户信息
        ActivityTsUser activityTsUser = activityTsUserMapper.selectByPrimaryKey(activityTsUserId);
        if(null == activityTsUser){
            res.setSuccess(false);
            res.setMessage(ActivityTsUserMess.USER_NOT_EXIST);
            return res;
        }
        DefindBeanUtils.copyProperties(activityTsUser, respExamDetail);

        //查找题目
        List<RespQuestion> respQuestionList = activityTsUserDetailMapper.findQuestionAnswerByUserId(activityTsUserId);
        respExamDetail.setRespQuestionList(respQuestionList);

        res.setSuccess(true);
        res.setData(respExamDetail);
        return res;
    }
}
